// import babel from '@rollup/plugin-babel'
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import typescript from 'rollup-plugin-typescript2'
import serve from 'rollup-plugin-serve'
import postcss from 'rollup-plugin-postcss'
import autoprefixer from 'autoprefixer'
import livereload from 'rollup-plugin-livereload'
import replace from '@rollup/plugin-replace'
import html from '@rollup/plugin-html'
import del from 'rollup-plugin-delete'
import copy from 'rollup-plugin-copy'
import htmlTemplate from './rollup/htmlTemplate'
import { getIPv4Address } from './rollup/rollup.common'
import log from './rollup/log'

// 开启rollup服务
export default {
  input: 'example/index.tsx',
  external: [ // 会在外部引入的组件
    'react',
    'react-dom',
  ],
  output: {
    file: 'exampleServer/index.js',
    format: 'umd',
    name: 'easy.swipe',
    globals: { // 引入的全局变量
      react: 'React',
      'react-dom': 'ReactDOM',
    }
  },
  cache: true,
  watch: {
    include: ['example/**', 'src/**']
  },
  plugins: [
    log(), // 编译日志
    del({ // 删除目录
      targets: 'exampleServer',
      runOnce: true
    }),
    replace({
      'process.env.NODE_ENV': '"production"',
      preventAssignment: false,
    }),
    commonjs(),
    nodeResolve(),
    typescript({
      clean: true,
      useTsconfigDeclarationDir: true,
    }),
    // babel({
    //   extensions: [
    //     '.js',
    //     '.jsx',
    //     '.ts',
    //     '.tsx'
    //   ],
    //   babelHelpers: 'bundled'
    // }),
    postcss({
      plugins: [autoprefixer],
      extract: 'index.css'
    }),
    html({
      title: 'react-easy-component',
      meta: [
        { charset: 'utf-8' },
        { name: 'viewport', content: 'width=device-width,maximum-scale=1.0,minimum-scale=1.0,initial-scale=1.0,user-scalable=no' },
        { name: 'renderer', content: 'webkit' },
        { 'http-equiv': 'Expires', content: '0' },
        { 'http-equiv': 'Pragma', content: 'no-cache' },
        { 'http-equiv': 'Cache-control', content: 'no-cache' }
      ],
      attributes: {
        html: { lang: 'en' },
        script: [ // 此选项被dsh重写 作用见htmlTemplate.js
          { type: 'div', attr: { id: 'app' } },
          // { type: 'script', attr: { src: 'https://cdn.bootcdn.net/ajax/libs/vConsole/3.3.4/vconsole.min.js' } },
          { type: 'script', attr: { src: 'https://cdn.bootcdn.net/ajax/libs/react/16.14.0/umd/react.development.js' } },
          { type: 'script', attr: { src: 'https://cdn.bootcdn.net/ajax/libs/react-dom/16.14.0/umd/react-dom.development.js' } },
          // { type: 'script', attr: { src: 'https://cdn.bootcdn.net/ajax/libs/react-router-dom/5.2.0/react-router-dom.js' } }
        ]
      },
      template: htmlTemplate
    }),
    copy({ // 拷贝文件
      targets: [
        { src: 'example/favicon.ico', dest: 'exampleServer' }
      ]
    }),
    serve({ // localhost服务
      contentBase: ['exampleServer'],
      historyApiFallback: true,
      host: 'localhost',
      port: 5187
    }),
    serve({ // 本机ip服务
      open: true,
      contentBase: ['exampleServer'],
      historyApiFallback: true,
      host: getIPv4Address(),
      port: 5187
    }),
    livereload()
  ]
}
